<?php
namespace app\member\controller;
use app\AdminRest;
use app\member\model\Coupon;
use app\member\model\Goods;
use app\member\model\Rights;
use app\member\model\RightsRelation;
use app\member\model\StoredLevel;
use app\shop\model\AdminCoupon;
use longbingcore\wxcore\WxSetting;
use think\App;
use app\member\model\Level as model;
use think\facade\Db;


class AdminLevel extends AdminRest
{

    protected $model;


    public function __construct(App $app) {

        parent::__construct($app);

        $this->model = new model();

    }


    /**
     * @author chenniang
     * @DataTime: 2020-07-15 10:49
     * @功能说明:会员等级列表
     */
    public function levelList(){

        $rights_model = new Rights();

        $rights_model->initData($this->_uniacid);

        $input = $this->_param;

        $type  = !empty($input['type'])?$input['type']:0;

        $dis = [

            'uniacid' => $this->_uniacid,

            'type'    => $type
        ];

        $data = $this->model->levelList($dis);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-07-15 11:21
     * @功能说明:会员等级详情
     */
    public function levelInfo(){

//        $rights_model = new Rights();

//        $rights_model->initData($this->_uniacid);

        $input = $this->_param;

        $dis = [

            'id' => $input['id']
        ];

        $data = $this->model->levelInfo($dis);

        if(empty($data)){

            $this->errorMsg('数据未找到');
        }
        $dis = [

            'uniacid' => $this->_uniacid,

            'type'    => 0

        ];
        //上一个等级
        $data['last_level_growth']  = $this->model->where($dis)->where('top','<',$data['top'])->order('top desc')->value('growth');
        //下一个等级
        $data['next_level_growth']  = $this->model->where($dis)->where('top','>',$data['top'])->order('top')->value('growth');

        $data['last_level_growth']  = !empty($data['last_level_growth'])?$data['last_level_growth']:0;

        $data['next_level_growth']  = !empty($data['next_level_growth'])?$data['next_level_growth']:9999999;
        //商品模型
        $goods_model = new Goods();
        //权益模型
        $rights      = new RightsRelation();
        //优惠券
        $coupon_model= new Coupon();
        //储值关联模型
        $stored_model= new StoredLevel();

        $dis = [

            'member_id' => $data['id']
        ];
        //商品
        $data['goods_id'] = $goods_model->where($dis)->column('goods_id');

        $data['goods_id'] = array_values($data['goods_id']);
        //储值
        $data['stored_id']= $stored_model->where($dis)->column('stored_id');

        $data['stored_id']= array_values($data['stored_id']);
        //优惠券
        $data['coupon']   = $coupon_model->where($dis)->select()->toArray();

        $data['coupon']   = array_values($data['coupon']);

        $dis['type']      = 1;
        //权益
        $data['rights']   = $rights->where($dis)->column('rights_id');

        $data['rights']   = array_values($data['rights']);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-07-15 13:24
     * @功能说明:
     */
    public function levelUpdate(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']
        ];

        $input['uniacid'] = $this->_uniacid;

        $data = $this->model->levelUpdate($dis,$input);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-07-17 13:42
     * @功能说明:会员商品
     */
    public function memberGoods(){

        $input = $this->_param;
        //商品
        $data['goods']  = $this->model->memberGoods($this->_uniacid,$input['id']);
        //储值
        $data['stored'] = $this->model->storedSelect($this->_uniacid,$input['id']);
        //自定义权益
        $data['rights'] = $this->model->memberRights($this->_uniacid,1);
        //默认权益
        $data['defult_rights'] = $this->model->memberRights($this->_uniacid);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-07-22 18:08
     * @功能说明:上下架
     */

    public function statusUpdate(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']
        ];

        $input['uniacid'] = $this->_uniacid;

        $input['status']  = $input['status']==1?0:1;

        Db::startTrans();

        $level = $this->model->levelInfo($dis);

        if($level['type']==0){

            $code = $this->model->checkStatus($input['id'],$input['status']);

            if($code['code']!=200){

                $this->errorMsg($code['msg']);

            }
        }

        $data = $this->model->where($dis)->update($input);

        Db::commit();

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-07-29 09:58
     * @功能说明:等级下拉框
     */
    public function levelSelect(){

        $dis = [

            'a.uniacid' => $this->_uniacid,

            'a.use_type'=> 1,

            'c.status'  => 1
        ];

        $model = new Rights();

        $data  = $model->goodsLevelSelect($dis);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-07-31 14:31
     * @功能说明:会员等级列表选择
     */
    public function memberLevelSelect(){

        $dis = [

            'uniacid' => $this->_uniacid,

            'status'  => 1

        ];

        $data = $this->model->where($dis)->order('top')->select()->toArray();

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-07 14:45
     * @功能说明:会员优惠券下拉框
     */
    public function memberCouponSelect(){

        $input = $this->_param;

        $coupon_model = new AdminCoupon();

        $dis = [

            'status' => 1,

            'uniacid'=> $this->_uniacid,

            'send_type'=>2
        ];

        $where = [];

        if(!empty($input['title'])){

            $where[] = ['title','like','%'.$input['title'].'%'];

        }

        $data = $coupon_model->where($dis)->where($where)->order('top desc')->paginate($input['limit'])->toArray();

        return $this->success($data);

    }













}
